#include "Camera.h"

Camera::Camera(int id, int targetId)
{
	position.Set(0, 0, 0);
	roll = 0.0f;

	this ->id = id;
	this ->target = new CameraTarget(targetId);
}

Camera::~Camera()
{
	if (target != NULL)
		delete target;
}

void Camera::Process(float ms)
{
}

void Camera::SetViewMatrix()
{
	Vec3 camTarget = target ->GetPosition() - position;
	camTarget.Normalize();
	camTarget = position + camTarget;
	//gluLookAt(camTarget.x, camTarget.y, camTarget.z, position.x, position.y, position.z, 0, 1, 0);
	sm::Vec3 up(0, 1, 0);
	up.RotateZ(roll);
	gluLookAt(position.x, position.y, position.z, camTarget.x, camTarget.y, camTarget.z, up.x, up.y, up.z);

	//glTranslatef(-position.x, -position.y, -position.z);
}

void Camera::SetInverseViewMatrix()
{
	Vec3 camTarget = target ->GetPosition() - position;
	camTarget.Normalize();
	camTarget = position + camTarget;
	//gluLookAt(camTarget.x, camTarget.y, camTarget.z, position.x, position.y, position.z, 0, 1, 0);
	sm::Vec3 up(0, 1, 0);
	up.RotateZ(-roll);
	gluLookAt(-position.x, -position.y, -position.z, -camTarget.x, -camTarget.y, -camTarget.z, up.x, up.y, up.z);

	//glTranslatef(-position.x, -position.y, -position.z);
}

Vec3 Camera::GetPosition()
{
	return position;
}

Vec3 Camera::GetTargetPosition()
{
	return target ->GetPosition();
}

float Camera::GetRoll()
{
	return roll;
}

float Camera::GetTargetDistance()
{
	Vec3 camTarget = target ->GetPosition();
	Vec3 v = camTarget - position;
	return v.GetLength();
}

void Camera::SetPosition(Vec3 position)
{
	this ->position = position;
}

void Camera::SetLookTarget(Vec3 lookTarget)
{
	this ->target ->SetPosition(lookTarget);
}

void Camera::SetRoll(float roll)
{
	this ->roll = roll;
}

void Camera::SetRotation(float angle, sm::Vec3 axis)
{
}

void Camera::SetScale(sm::Vec3 scale)
{
}

int Camera::GetId()
{
	return id;
}

CameraTarget * Camera::GetTarget()
{
	return target;
}